<template>
  <div class="sku-list">
    <el-card>
      <el-table style="width: 100%" border :data="skuList" v-loading="loading">
        <el-table-column type="index" label="序号" width="80" align="center">
        </el-table-column>
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="skuDesc" label="描述" width="width">
        </el-table-column>
        <el-table-column
          prop="prop"
          label="默认图片"
          width="150"
          align="center"
        >
          <template slot-scope="{ row, $index }">
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="重量(KG)" width="width">
        </el-table-column>
        <el-table-column
          prop="price"
          label="价格(元)"
          width="80"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="prop" label="操作" width="width" align="center">
          <template slot-scope="{ row, $index }">
            <HintButton
              v-if="row.isSale === 0"
              type="info"
              icon="el-icon-top"
              size="mini"
              title="上架"
              @click="topSku(row)"
            ></HintButton>
            <HintButton
              v-if="row.isSale === 1"
              type="success"
              icon="el-icon-bottom"
              size="mini"
              title="下架"
              @click="downSku(row)"
            ></HintButton>

            <HintButton
              type="primary"
              icon="el-icon-edit"
              size="mini"
              title="修改"
              @click="updateSku(row)"
            ></HintButton>
            <HintButton
              type="info"
              icon="el-icon-info"
              size="mini"
              title="查看详情"
              @click="seeDetail(row)"
            ></HintButton>
            <HintButton
              type="danger"
              icon="el-icon-delete"
              size="mini"
              title="删除"
              @click="removeSku(row)"
            ></HintButton>
          </template>
        </el-table-column>
      </el-table>
      <!-- 
  @size-change="handleSizeChange"
        @current-change="handleCurrentChange" -->
      <el-pagination
        background
        style="text-align: center"
        :current-page="page"
        :page-sizes="[5, 10, 20, 30, 40, 50]"
        :page-size="limit"
        :pager-count="7"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
        @current-change="getInitSkuList"
        @size-change="handleSizeChange"
      >
      </el-pagination>

      <el-drawer
        title="我是标题"
        :visible.sync="drawer"
        :direction="direction"
        :with-header="false"
        size="50%"
      >
        <el-row>
          <el-col :span="5">名称</el-col>
          <el-col :span="16">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">描述</el-col>
          <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">价格</el-col>
          <el-col :span="16">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              type="success"
              v-for="(skuAttrValue, index) in skuInfo.skuAttrValueList"
              :key="skuAttrValue.id"
            >
              {{ `${skuAttrValue.attrId}-${skuAttrValue.valueId}` }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">商品图片</el-col>
          <el-col :span="16">
            <el-carousel class="sku-carousel" height="400px" trigger="click">
              <el-carousel-item
                v-for="(skuImg, index) in skuInfo.skuImageList"
                :key="skuImg.id"
              >
                <img :src="skuImg.imgUrl" alt="" />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "SkuList",
  data() {
    return {
      drawer: false,
      direction: "rtl",
      loading: false,
      page: 1,
      limit: 5,
      total: 0,
      skuList: [],
      skuInfo: {},
    };
  },
  mounted() {
    this.getInitSkuList();
  },
  methods: {
    //初始化数据展示以及页面的切换
    async getInitSkuList(page = 1) {
      this.page = page;
      this.loading = true;
      try {
        const result = await this.$API.sku.getList(this.page, this.limit);
        // console.log(result);
        if (result.code === 20000 || result.code === 200) {
          this.skuList = result.data.records;
          this.total = result.data.total;
        } else {
          this.$message.error("获取sku分页列表失败");
        }
      } catch (error) {
        this.$message.error("请求获取sku分页列表失败");
      }
      this.loading = false;
    },
    //可选页面
    handleSizeChange(limit) {
      this.limit = limit;
      this.getInitSkuList();
    },
    //点击上架
    async topSku(row) {
      try {
        const result = await this.$API.sku.onSale(row.id);
        if (result.code === 20000 || result.code === 200) {
          this.$message.success("上架成功");

          this.getInitSkuList(this.page);
        } else {
          this.$message.error("上架失败");
        }
      } catch (error) {
        this.$message.error("请求上架失败");
      }
    },
    // 点击下架
    async downSku(row) {
      try {
        const result = await this.$API.sku.cancelSale(row.id);
        if (result.code === 20000 || result.code === 200) {
          this.$message.success("下架成功");

          this.getInitSkuList(this.page);
        } else {
          this.$message.error("下架失败");
        }
      } catch (error) {
        this.$message.error("请求下架失败");
      }
    },
    //点击修改
    async updateSku(row) {
      this.$message.info("正在开发中");
    },
    // 点击删除
    async removeSku(row) {
      try {
        const result = await this.$API.sku.remove(row.id);
        if (result.code === 20000 || result.code === 200) {
          this.$message.success("删除sku成功");
          this.getInitSkuList(
            this.skuList.length > 1 ? this.page : this.page - 1
          );
        } else {
          this.$message.error("删除sku失败");
        }
      } catch (error) {
        this.$message.error("请求删除sku失败");
      }
    },
    //点击查看详情
    async seeDetail(row) {
      this.drawer = true;
      try {
        const result = await this.$API.sku.get(row.id);
        // console.log(result);
        if (result.code === 20000 || result.code === 200) {
          this.$message.success("获取sku详情成功");
          this.skuInfo = result.data;
        } else {
          this.$message.error("获取sku详情失败");
        }
      } catch (error) {
        this.$message.success("请求获取sku详情失败");
      }
    },
  },
};
</script>



<style lang='scss' scoped>
.sku-list {
  .el-row {
    height: 40px;
    .el-col {
      line-height: 40px;
      &.el-col-5 {
        font-size: 18px;
        font-weight: bold;
        text-align: right;
        margin-right: 20px;
      }
    }
  }
  .sku-carousel {
    width: 400px;
    border: 1px solid #ccc;
    img {
      width: 400px;
      height: 400px;
    }
    ::v-deep .el-carousel__indicator {
      button {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background-color: red;
      }
      &.is-active {
        button {
          background: green;
        }
      }
    }
  }
}
</style>